// 登录注册和忘记密码三个页面跳转
var box=document.getElementById("box")
var box_reg=document.getElementById("box_reg")
var reg = document.getElementById("reg")
var forit = document.getElementById("forit")
function regnow(){
    box.style.display="none";
    box_reg.style.display="block";
}
function forgot_code(){
    box.style.display="none";
    forit.style.display="block";
}
//当点击注册账号后想起自己已有账号时返回登录页面
var box=document.getElementById("box")
var box_reg=document.getElementById("box_reg")
var haven = document.getElementById("haven")
haven.onclick=function(){
    box.style.display="block";
    box_reg.style.display="none";
}
//当点击忘记密码后突然想起密码返回登录页面
var box=document.getElementById("box")
var forit = document.getElementById("forit")
var back = document.getElementById("back")
back.onclick = function(){
    box.style.display="block";
    forit.style.display="none";
}
// 小眼睛是否查看密码
var img1 = document.getElementById("img1");
var password = document.getElementById("password");
img1.onclick = function () {
    if (img1.getAttribute('src') == "../../Img/eye.png") {
        img1.src = "../../Img/eyes.png";
        password.type = 'text';
    } else {
        img1.src = "../../Img/eye.png";
        password.type = 'password';
    }
}
var img2 = document.getElementById("img2");
var passwd = document.getElementById("passwd");
img2.onclick = function () {
    if (img2.getAttribute('src') == "../../Img/eye.png") {
        img2.src = "../../Img/eyes.png";
        passwd.type = 'text';
    } else {
        img2.src = "../../Img/eye.png";
        passwd.type = 'password';
    }
}
var img3 = document.getElementById("img3");
var passor = document.getElementById("passor");
img3.onclick = function () {
    if (img3.getAttribute('src') == "../../Img/eye.png") {
        img3.src = "../../Img/eyes.png";
        passor.type = 'text';
    } else {
        img3.src = "../../Img/eye.png";
        passor.type = 'password';
    }
}

//登录
var email=document.getElementById("email")
var password=document.getElementById("password")
var login_btn = document.getElementById("login_btn")
login_btn.onclick=function() {
    var em = email.value;
    var pa = password.value;


//判断邮箱格式是否符合要求
    if(em !=""){
        var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        //调用正则验证test()函数
        isok=reg.test(em);
        if(!isok){
            alert("邮箱格式不正确，请重新输入！");
            return false;
            // window.location.href="/User/login";
        }else{
            // alert("邮箱格式正确！")
            //判断邮箱位数是否正确
            if(em != "") {
                var reg = /^\d{6,18}@[qQ][qQ]\.(com|cn)$/;
                isok = reg.test(em);
                if (!isok) {
                    alert("邮箱位数不正确，请重新输入！");
                    return false;
                } else {
                    /////判断邮箱是否存在
                    $.ajax({
                        type: 'get',
                        data: {
                            email: em,
                        },
                        url: "/User/getEmailFlag",
                        success: function (data) {
                            console.log(data);
                            //判断密码位数是否符合要求
                            if (pa < 100000 || pa > 9999999999999999) {
                                alert("密码位数不正确，请重新输入！")
                                return false;
                            } else {
                                // alert("密码位数正确！")
                                //////判断邮箱和密码是否相互对应
                                $.ajax({
                                    type:'post',
                                    data:{
                                        email:em,
                                    },
                                    url:"/User/getPassword",
                                    success:function (data) {
                                        //alert("用户存在")
                                        console.log(data)
                                        console.log(JSON.parse(data))
                                        var pp = JSON.parse(data);
                                        var ppp=pp.password;
                                        if(ppp===pa){
                                            //////登录
                                            $.ajax({
                                                type: 'post',
                                                data: {
                                                    email: em,
                                                    password: pa,
                                                },
                                                url: "/User/loginUser",
                                                success: function (data) {
                                                    console.log(data);
                                                    let em = email.value;
                                                    $.ajax({
                                                        type: "post",
                                                        url: '/User/getFlag',
                                                        data: {
                                                            email: em,
                                                        },
                                                        success: function (data) {
                                                            console.log(data);
                                                            var pp = JSON.parse(data);
                                                            console.log(pp);
                                                            console.log(pp.flag);
                                                            var fg = pp.flag;
                                                            console.log(fg);
                                                            var flag = pp.flag
                                                            localStorage.setItem('key',flag);
                                                            if (fg != 2) {
                                                                console.log(fg)
                                                                window.location.href = "/";
                                                            } else {
                                                                console.log(fg)
                                                                window.location.href = "/admin/login";
                                                            }
                                                        },
                                                        error:function(){

                                                        }

                                                    })

                                                },
                                                error: function () {
                                                    alert("登录失败，请正确输入邮箱和密码！")
                                                }
                                            })
                                        }else{
                                            alert("不存在")
                                        }
                                    },
                                    error:function(){
                                        alert("用户不存在！")
                                    }
                                })
                            }
                        },
                        error: function () {
                            alert("该邮箱不存在,请重新输入！")
                        }
                    })
                }
            }
        }

    }
}



//注册新账号
var name = document.getElementById("name")
var ema = document.getElementById("ema")
var send = document.getElementById("send")
//邮箱和验证码
send.onclick = function () {
    //////判断邮箱是否正确
    var em = ema.value;
    if(em != "") {
        var reg = /^\d{6,18}@[qQ][qQ]\.(com|cn)$/;
        isok = reg.test(em);
        if (!isok) {
            alert("邮箱格式不正确，请重新输入！");
            return false;
        } else {
            ///////判断邮箱是否存在
            $.ajax({
                type: 'get',
                data: {
                    email: em,
                },
                url: "/User/getEmailFlag",
                success: function (data) {
                    console.log(em);
                    ///////发送验证码
                    $.ajax({
                        type: 'post',
                        data: {
                            email: em,
                        },
                        url: "/User/sendAuthCodeEmail",
                        success: function (data) {
                            console.log(data);
                            /////alert("验证码已发送到您的邮箱");
                            $("#send").click(function () {
                                $(this).attr({"disabled":"disabled"});
                            })
                            return false;
                        },
                        error: function () {
                            alert("验证码发送失败!")
                            return false;
                        }
                    })
                },
                error: function (data) {
                    alert("该邮箱不存在！")
                    return false;
                }
            })
        }
    }
}
/////点击注册时判断
////判断用户名是否符合要求
//////判断输入的验证码和发送的验证码是否一致

var register = document.getElementById("register")
register.onclick = function(){
    var passwd = document.getElementById("passwd")
    var name = document.getElementById("name")
    var code = document.getElementById("code")
    var ema = document.getElementById("ema")
    var na = name.value;
    var em = ema.value;
    console.log(na)
    console.log(name.value)
    $.ajax({
        type:'post',
        data:{
            email:em,
        },
        url:"/User/getEmailCard",
        success:function(data){
            console.log(data);
            var cd = code.value;
            console.log(cd);
            var ee = JSON.parse(data);
            var eee=ee.email;
            if(eee===cd){
                //alert("验证码符合")

            }else{
                alert("验证码不吻合！")
            }
//////判断密码位数是否符合要求
            var cd = code.value;

            var em = ema.value;
            var pd = passwd.value;
            if(pd < 100000 || pd > 9999999999999999){
                alert("密码不符合要求！")
            }else{
                ///////  注册
                $.ajax({
                    type:'post',
                    data:{
                        code:cd,
                        email:em,
                        nickname:na,
                        password:pd,
                    },
                    url:"/User/addUser",
                    success:function(data){
                        console.log(cd)
                        console.log(em)
                        console.log(na)
                        console.log(pd)
                        alert("注册成功")
                        window.location.href = "/User/login";
                        $('#send').removeAttribute('disabled')
                    },
                    error:function(){
                        alert("注册失败！！！")
                        return false;
                    }
                })
            }
        },
        error:function () {
            alert("验证码错误！")
            return false;

        }
    })
}

///////////忘记密码
/////判断邮箱是否存在啊
var reset = document.getElementById("reset")
var cod = document.getElementById("cod")
var passor = document.getElementById("passor")
var emai = document.getElementById("emai")
var reGet = document.getElementById("reGet")
reGet.onclick = function () {
    //////判断邮箱是否正确
    var em = emai.value;
    if (em != "") {
        var reg = /^\d{6,18}@[qQ][qQ]\.(com|cn)$/;
        isok = reg.test(em);
        if (!isok) {
            alert("邮箱格式不正确，请重新输入！");
            return false;
        } else {
            ///////判断邮箱是否存在
            $.ajax({
                type: 'get',
                data: {
                    email: em,
                },
                url: "/User/getEmailFlag",
                success: function (data) {
                    console.log(em);
                    // alert("邮箱存在！")
                    ////////获取验证码
                    //忘记密码时候获取验证码
                    var ee = emai.value;
                    $.ajax({
                        type:'post',
                        data:{
                            email:ee,
                        },
                        url:"/User/sendAuthCodeEmail",
                        success:function(data){
                            console.log(data);
                            ///////alert("验证码已发送到您的邮箱");
                            /////重置密码
                            $("#reGet").click(function () {
                                $(this).attr({"disabled":"disabled"});
                            })
                            reset.onclick = function(){
                                var cc = cod.value;
                                var pass = passor.value;
                                var ee = emai.value;
                                ///////判断新密码是否符合要求
                                var po = passor.value;
                                if(po < 100000 || po > 9999999999999999){
                                    alert("密码不符合要求！")
                                    return false;
                                }else{
                                    ////////////////判断验证码是否正确
                                    var cd =cod.value;
                                    $.ajax({
                                        type: 'post',
                                        data: {
                                            email: em,
                                        },
                                        url: "/User/getEmailCard",
                                        success: function (data) {
                                            console.log(data);
                                            var ee = JSON.parse(data);
                                            var eee = ee.email;
                                            if (eee === cd) {
                                                // alert("验证码符合")
                                                $.ajax({
                                                    type:'post',
                                                    data:{
                                                        newCode:cc,
                                                        newEmail:ee,
                                                        newPassword:pass,
                                                    },
                                                    url:"/User/resetPassword",
                                                    success:function (data) {
                                                        console.log(data);
                                                        alert("重置密码成功！");
                                                        window.location.href="/User/login";
                                                        $('#reGet').removeAttribute('disabled')
                                                    },
                                                    error:function () {
                                                        alert("重置密码失败！请重试")
                                                        return false;
                                                    }
                                                })
                                            } else {
                                                alert("验证码不吻合！")
                                                return false;
                                            }
                                        }
                                    })
                                }
                            }
                        },
                        error:function(){
                            alert("验证码发送失败!")
                            return false;
                        }
                    })
                },
                error: function () {
                    alert("邮箱不存在")
                    return false;
                }
            })
        }
    }
}
